package com.example.test.serviceImpl;

import com.alibaba.fastjson.JSON;
import com.example.test.service.TaskService;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 定时任务调度测试
 * 
 * @author ruoyi
 */
@Component
public class TaskServiceImpl implements TaskService
{
    public void send(String id, String type) {
        Map<String, String> paramMap = new HashMap();
        paramMap.put("from","900000010");
        paramMap.put("client","GZ000007");
        if(type.equals("wybx")){
            paramMap.put("clientName","物业报修");
            paramMap.put("type","86");
            paramMap.put("title","物业报修");

        }else{
            paramMap.put("clientName","厦工小事");
            paramMap.put("type","86");
            paramMap.put("title","小事说");
        }
        if (type.equals("sendMessage"))
            paramMap.put("sub","您有小事说信息待阅，请前往我的小事说对话框查阅");
        else if (type.equals("handOver"))
            paramMap.put("sub","您有一条待处理小事，请前往待处理的小事里查阅");
        else if (type.equals("progressUpdate"))
            paramMap.put("sub","您的小事说有新的进展，请在我的小事说查看");
        else if (type.equals("cuiBan"))
            paramMap.put("sub","您有一条待处理小事，请前往\"小事说\"待处理的小事查办(催办)");
        paramMap.put("fromName","厦工小事");
//                storySysEmployee.getSysEmployeeId()
        paramMap.put("to", id);
        String result = doPost("http://fcim.xit.edu.cn:82/public/officialAccounts", paramMap);
        System.out.println("result = " + result);
    }



    public String doPost(String url, Map<String, String> param) {
        // 创建Httpclient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        String resultString = "";
        try {
            // 创建Http Post请求
            HttpPost httpPost = new HttpPost(url);
            httpPost.setHeader("Content-Type", "application/json;charset=utf8");
            // 创建参数列表
            if (param != null) {
                StringEntity entity = new StringEntity(JSON.toJSONString(param),"utf-8");
                entity.setContentEncoding("UTF-8");   //解决中文乱码问题
                entity.setContentType("application/json"); //解决中文乱码问题
                httpPost.setEntity(entity);
            }
            // 执行http请求
            response = httpClient.execute(httpPost);
            resultString = EntityUtils.toString(response.getEntity(), "utf-8");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                response.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        return resultString;
    }

    @Override
    public void send(String id, String type, String msg) {
        Map<String, String> paramMap = new HashMap();
        paramMap.put("from","900000010");
        paramMap.put("client","GZ000007");
        if(type.equals("wybx")){
            paramMap.put("clientName","物业报修");
            paramMap.put("type","86");
            paramMap.put("title","物业报修");
            paramMap.put("sub",msg);
        }
        paramMap.put("fromName","物业报修");
        paramMap.put("to", id);
        String result = doPost("http://fcim.xit.edu.cn:82/public/officialAccounts", paramMap);
        System.out.println("result = " + result);

    }
}
